package org.jcc.core.demo;

import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PatternPic extends JPanel {

	private static final long serialVersionUID = 1L;

	private Image image;

	public PatternPic(Image image) {
		this.image = image;
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		int width = image.getWidth(this);
		int height = image.getHeight(this);

		int lineCount = 4;
		Pattern pattern = new Pattern();

		for (int lineNumber = 0, y = 0; lineNumber < lineCount; lineNumber++, y += height) {
			// 直接获取各部分的数目
			int elementCountOfFirst = pattern.getElementCountOfFirstPart(lineCount, lineNumber);
			int elementCountOfSecond = pattern.getElementCountOfSecondPart(lineNumber);

			for (int i = 0, x = elementCountOfFirst * width; i < elementCountOfSecond; i++, x += width) {
				g.drawImage(image, x, y, width, height, this);
			}
		}
	}

	public static void main(String[] args) throws IOException {
		Image image = ImageIO.read(PatternPic.class.getResource("/logo-git-oschina.png"));
		JFrame frame = new JFrame();
		frame.add(new PatternPic(image));
		frame.setSize(800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}